package com.alstudio.module.xmpp.protocol.xmpp.parser.iq.voip;

import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALMessage;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.log.ALLog;

/**
 * 本类用于解析接听电话结果
 * 
 * @author alonso lee
 * 
 */
public class AcceptVoipCallParser extends ALBaseIQParser implements
		IQXmlParseEventCallback {

	private int code = 0;
	private String sessionid = null;
	private String ip = null;
	private int port = -1;

	private final String SESSION_TAG = "sessionid";
	private final String IP_TAG = "ip";
	private final String PORT_TAG = "port";
	private final String NAT_TAG = "nataddr";
	private final String LAN_TAG = "lanaddr";
	private final String TRANSFER_TAG = "turnaddr";
	private final String ERROR_TAG = "error";
	private ALMessage msg;

	@Override
	public void parseIQPackage(ALIQ packact, String stream, ALXmppEventListener listener)
			throws Exception {
		// TODO Auto-generated method stub
		code = 0;
		msg = null;
		sessionid = null;
		ip = null;
		port = -1;
		mListener = listener;

		startParse(packact, stream, this);
	}

	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ERROR_TAG)) {
			code = ALNumericUtils.parseInt(getAttValue("code"), 0);
			ALLog.e("accept call code error: " + code);
		}
	}

	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if(msg == null){
			msg = new ALMessage();
		}
		if (tag.equals(SESSION_TAG)) {
			sessionid = readText();
			ALLog.d("current session " + sessionid);
		} else if (tag.equals(NAT_TAG)) {
			ip = getAttValue(IP_TAG);
			port = ALNumericUtils.parseInt(getAttValue(PORT_TAG), 4000);
			msg.setNatIp(ip);
			msg.setNatPort(port);
			ALLog.d(NAT_TAG + " ip " + ip + " port " + port);
		} else if (tag.equals(LAN_TAG)) {
			ip = getAttValue(IP_TAG);
			port = ALNumericUtils.parseInt(getAttValue(PORT_TAG), 4000);
			msg.setLanIp(ip);
			msg.setLanPort(port);
			ALLog.d(LAN_TAG+" ip " + ip + " port " + port);
			
		} else if (tag.equals(TRANSFER_TAG)) {
			ip = getAttValue(IP_TAG);
			port = ALNumericUtils.parseInt(getAttValue(PORT_TAG), 4000);
			msg.setTransferIp(ip);
			msg.setTransferPort(port);
			ALLog.d("ip " + ip + " port " + port);
		}
	}

	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if (mListener != null) {
			mListener.onGetAcceptVoipCallResult(code, sessionid, msg);
		}
	}

}
